$(function () {
    $("#jqGrid").Grid({
        url: '../moduletype/list',
        colModel: [
            {label: '类型编码', name: 'typeCode', index: 'TYPE_CODE', width: 80},
            {label: '类型名称', name: 'typeName', index: 'TYPE_NAME', width: 80},
            {label: '上级类型', name: 'parentName', index: 'SUPER_TYPE', width: 80},
            {
                label: '位置管理', width: 30, align: 'center', sortable: false,
                formatter: function (value, col, row) {
                    return "<button title='详情' class='btn btn-outline btn-info' onclick='vm.managePosition(\"" + row.id + "\")'><i class='fa fa-file-text-o'></i></button>";
                }
            },
            {name: 'id', key: true, hidden: true}
        ]
    });
    vm.getTypeTree();
});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "superType",
            rootPId: 0
        },
        key: {
            url: "nourl",
            name: "typeName" //name 与 typeName 绑定
        },
    },
    callback: {
        onClick: function (event,treeId,treeNode,clickFlag) {
            vm.q.id = treeNode.id;
            vm.query()
        }
    }
};

var typeTree;

var setting2 = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "superType",
            rootPId: 0
        },
        key: {
            url: "nourl",
            name: "typeName" //name 与 typeName 绑定
        },
    }
};

var typeTree2;

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        moduleType: {superType: '', typeName: '', typeCode: '', id: '', parentName: ''},
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: '',
            id: ''
        }
    },
    methods: {
        /**
         * 查看模具详情
         */
        managePosition: function (moldTypeId,typeName) {
            openWindow({
                type: 2,
                area:['100%','100%'],
                title: '<i class="fa fa-bars"></i> 模具位置关联设置',
                content: '../module/moldTypePosition.html?moldTypeId=' + moldTypeId
            })
        },
        getTypeTree: function () {
            //加载故障类型树
            Ajax.request({
                url: "../moduletype/select",
                async: true,
                successCallback: function (r) {
                    var tree = r.typeList;
                    typeTree = $.fn.zTree.init($("#typeTree"), setting, tree);
                    typeTree.expandAll(true);
                }
            });
        },
        getModuleTypeTree: function () {
            Ajax.request({
                url: "../moduletype/select",
                async: true,
                successCallback: function (r) {
                    var tree = r.typeList;

                    typeTree2 = $.fn.zTree.init($("#moduleTypeTree"), setting2, tree);

                    typeTree2.expandAll(true);
                    // console.log(vm.toolsBase);
                    var node = typeTree2.getNodeByParam("id", vm.moduleType.id);
                    // console.log(node);
                    if (node) {
                        typeTree2.selectNode(node);
                        if (node.parentName) {
                            vm.moduleType.parentName = node.parentName;
                        } else {
                            vm.moduleType.parentName = '无';
                        }
                    } else {
                        node = typeTree2.getNodeByParam("id", "0");
                        typeTree2.selectNode(node);
                        // vm.logicCourse.courseTypeName = node.name;
                    }

                }
            });
        },
        typeTree: function () {
            openWindow({
                title: "选择模具类型",
                area: ['300px', '450px'],
                content: jQuery("#moduleTypeLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = typeTree2.getSelectedNodes();
                    if (node.length != 0) {
                        vm.moduleType.superType = node[0].id;
                        vm.moduleType.parentName = node[0].typeName;
                    } else {
                        vm.moduleType.superType = 0;
                        vm.moduleType.parentName = '未选择';
                    }

                    layer.close(index);
                }
            });
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.moduleType = {superType: '', typeName: '', typeCode: '', parentName: ''};
            vm.getModuleTypeTree();
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
            vm.getModuleTypeTree();
        },
        saveOrUpdate: function (event) {
            var url = vm.moduleType.id == null ? "../moduletype/save" : "../moduletype/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.moduleType),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../moduletype/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../moduletype/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.moduleType = r.moduleType;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: vm.q,
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: '',
                id:''
            };
            typeTree.cancelSelectedNode();
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});
